package com.jindu.producer.finance.controller;

import com.jindu.kernel.result.JsonResult;
import com.jindu.kernel.utils.AlphaBeanUtils;
import com.jindu.kernel.utils.DateUtils;
import com.jindu.kernel.utils.IdGenerator;
import com.jindu.producer.finance.api.DictionaryInterface;
import com.jindu.producer.finance.jpa.FiscalResultJpa;
import com.jindu.producer.finance.model.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jarvis
 * @date 2018/10/18
 */
@Api(description = "财税评测结果管理", tags = "FiscalResultController", basePath = "/fiscalResult")
@RestController
@RequestMapping(value = "/fiscalResult",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class FiscalResultController {

    @Autowired
    FiscalResultJpa fiscalResultJpa;

    @Autowired
    DictionaryInterface dictionaryInterface;


    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "根据财税评测结果的ID，查询财税评测结果的信息", notes = "返回单个财税评测结果的信息")
    @RequestMapping(value = "get",method = RequestMethod.GET)
    public JsonResult<FFiscalResultEntity> get(@RequestParam String id){
        return JsonResult.getSuccessResult(fiscalResultJpa.findById(id));
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "根据企业的ID，查询财税评测结果的信息", notes = "返回单个财税评测结果的信息")
    @RequestMapping(value = "getByOrgId",method = RequestMethod.GET)
    public JsonResult<List<FiscalAndBase>> getByOrgId(@RequestParam Integer orgId){

        List<FiscalAndBase> fiscalAndBases=new ArrayList<>();

        List<DictionaryEntity> question=dictionaryInterface.getListByParentId("d5ca5543-2e76-11e8-9be1-00163e0c3e80");
        List<FFiscalResultEntity> answer=fiscalResultJpa.findByOrgId(orgId);

        for (DictionaryEntity q : question) {

            FiscalAndBase fq=new FiscalAndBase();

            fq.setSortId(q.getSortId());
            fq.setQuestionName(q.getName());

            FFiscalResultEntity model=new FFiscalResultEntity();

            for (FFiscalResultEntity as : answer) {
                if(as.getItemId()==q.getSortId()){
                    fq.setId(as.getId());
                    fq.setItemId(as.getItemId());
                    fq.setOrgId(as.getOrgId());
                    fq.setUserId(as.getUserId());
                    fq.setValue(as.getValue());
                    fq.setValue2(as.getValue2());
                }
            }
            fiscalAndBases.add(fq);
        }

        return JsonResult.getSuccessResult(fiscalAndBases);

    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "保存财税评测结果信息", notes = "保存财税评测结果的信息")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public JsonResult<FFiscalResultEntity> save(@RequestBody FFiscalResultEntity fiscalResultEntity){

        FFiscalResultEntity fFiscalResultEntity=fiscalResultJpa.findById(fiscalResultEntity.getId());

        if(fFiscalResultEntity == null){
            fiscalResultEntity.setId(IdGenerator.uuid());
            fiscalResultEntity.setCreateTime(new Timestamp(DateUtils.getCurrentTime()));
            fiscalResultEntity.setModifyTime(new Timestamp(DateUtils.getCurrentTime()));
            return JsonResult.getSuccessResult(fiscalResultJpa.saveAndFlush(fiscalResultEntity));
        }else {
            fiscalResultEntity.setModifyTime(new Timestamp(DateUtils.getCurrentTime()));
            BeanUtils.copyProperties(fiscalResultEntity,fFiscalResultEntity, AlphaBeanUtils.getNullPropertyNames(fiscalResultEntity));
            return JsonResult.getSuccessResult(fiscalResultJpa.saveAndFlush(fFiscalResultEntity));
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "批量保存财税填写信息", notes = "批量保存财税录入的信息")
    @RequestMapping(value = "saveBatch", method = RequestMethod.POST)
    public JsonResult<Integer> saveBatch(@RequestBody List<FFiscalResultEntity> entities){

        if(entities.size()>0){
            int num=0;
            for (FFiscalResultEntity entity : entities){

                if(entity.getItemId()!=null&&entity.getOrgId()!=null){

                    FFiscalResultEntity fFiscalResultEntity=fiscalResultJpa.findByOrgIdAndItemId(entity.getOrgId(),entity.getItemId());

                    if(fFiscalResultEntity==null){
                        entity.setId(IdGenerator.uuid());
                        entity.setCreateTime(new Timestamp(DateUtils.getCurrentTime()));
                        entity.setModifyTime(new Timestamp(DateUtils.getCurrentTime()));
                        fiscalResultJpa.saveAndFlush(entity);
                        num++;
                    }else {
                        entity.setModifyTime(new Timestamp(DateUtils.getCurrentTime()));
                        BeanUtils.copyProperties(entity,fFiscalResultEntity, AlphaBeanUtils.getNullPropertyNames(entity));
                        fiscalResultJpa.saveAndFlush(fFiscalResultEntity);
                        num++;
                    }
                }
            }
            return JsonResult.getSuccessResult(num);

        }else {
            return JsonResult.getFailureResult();
        }

    }



    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "删除财税评测结果的信息", notes = "根据ID,删除数据")
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public JsonResult<Integer> delete(@RequestBody String id){
        return JsonResult.getSuccessResult(fiscalResultJpa.deleteById(id));
    }




}
